package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.jiyun.pojo.ThTour;
import com.jiyun.pojo.ThTourCompleted;
import com.jiyun.mapper.ThTourCompletedMapper;
import com.jiyun.service.IThTourCompletedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.service.IThTourService;
import common.models.dtos.DaKaDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-10-27
 */
@Service
public class ThTourCompletedServiceImpl extends ServiceImpl<ThTourCompletedMapper, ThTourCompleted> implements IThTourCompletedService {

    @Autowired
    private IThTourService thTourService;
    /**
     * 对巡店任务打卡
     * @param daKaDto
     * @return
     */
    @Override
    @Transactional
    public boolean daka(DaKaDto daKaDto) {
        if (daKaDto==null){
            throw new RuntimeException("参数为空");
        }
        ThTourCompleted thTourCompleted=new ThTourCompleted();
        thTourCompleted.setStoreId(daKaDto.getStoreId());
        thTourCompleted.setTourTask(daKaDto.getRemarks());
        thTourCompleted.setTourTask(daKaDto.getTourTask());
        this.save(thTourCompleted);
        //更新巡店任务状态
        Integer tourId = daKaDto.getTourId();
        if (tourId==null){
            throw new RuntimeException("巡店任务id为空");
        }
        LambdaQueryWrapper<ThTour> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ThTour::getId,tourId);
        ThTour thTour = thTourService.getOne(queryWrapper);
        if (thTour==null){
            throw new RuntimeException("查询不到这个任务");
        }
        thTour.setWc(1);
        thTourService.updateById(thTour);
        return true;
    }
}
